package com.wjy.sfhcore.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

import com.wjy.sfhcore.R;
import com.wjy.sfhcore.util.ToastUtil;

/**
 * Created by wangjingyuan on 15/6/2.
 */
public abstract class AbsTabsFragment extends CoreFragment implements TabHost.OnTabChangeListener {
    protected FragmentTabHost mTabHost;
    protected int currentIndex;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tabs, container, false);
        mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
        mTabHost.setup(baseActivity, getChildFragmentManager(), R.id.realtabcontent);
        addTabs();
        mTabHost.setOnTabChangedListener(this);
        return view;
    }

    /**
     * 设置Tab切换
     *
     * @param tabIndex 切换的Tab下标
     */
    protected void setTabChange(int tabIndex) {
        mTabHost.setCurrentTab(tabIndex);
    }

    /**
     * 子类需要实现add tab的方法
     * 如：mTabsAdapter.addTab(mTabHost.newTabSpec("custom").setIndicator
     * ("Custom"), LoaderCourseListSupport.CourseListFragment.class, null);
     */
    protected abstract void addTabs();

    /**
     * 添加Tab子页面
     *
     * @param tabView Tab导航试图
     * @param cls     Tab页Class对象
     * @param bundle  传递参数
     */
    protected void addTab(View tabView, Class<?> cls, Bundle bundle) {
        mTabHost.addTab(
                mTabHost.newTabSpec(cls.getSimpleName()).setIndicator(tabView),
                cls, bundle);
    }

    /**
     * 获取当前Tab下标
     *
     * @return Tab下标
     */
    protected int getTabPosition() {
        return currentIndex;
    }

    public void onTabChanged(String tabId) {
        currentIndex = mTabHost.getCurrentTab();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }

    @Override
    public boolean isInitToolBar() {
        return false;
    }
}
